import  time
from common.Util import BlogDriver
from selenium.webdriver.common.by import By

# 博客列表 页
class BlogList:
    driver=""
    url=""
    def __init__(self):
        # Edge浏览器驱动对象
        self.driver=BlogDriver.driver
        # 博客系统列表网页
        self.url="http://8.137.19.140:9090/blog_list.html"
        # 进入列表页面
        self.driver.get(self.url)

    # 在登录状态下
    def BlogLoginListTest(self):
        # 检查一下博客的标题是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title")
        # 检查一下发布博客的时间是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.date")
        # 检查一下博客的内容是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.desc")
        # 检查一下博客列表页的查看全文按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > a")
        # 检查一下用户基本信息的昵称是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        # 检查一下用户基本信息的文章是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        # 检查一下用户基本信息的分类是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
        # 检查一下博客数量是否为0，不为就就通过
        Blognum = self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)").text
        # 打印文章数量
        print(Blognum)
        assert Blognum != 0
        # 截屏
        BlogDriver.GetScreeShot()

    # 未登录状态下
    def NotBlogLoginListTest(self):
        # 找到用户命名输入框和密码输入框把里面的内容清空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 在未登录的状态，直接进入博客列表页
        self.driver.get("http://8.137.19.140:9090/blog_list.html")
        time.sleep(2)
        # 截图页面
        BlogDriver.GetScreeShot()
        # 查看是否还有博客标题
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")

    # 登录博客状态下测试退出
    def QuitBlogLoginListText(self):
        # 点击退出按钮
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        time.sleep(1)
        # 截屏页面
        BlogDriver.GetScreeShot()
        # 判断一下退出后的页面url和页面的标题
        Quittitle = self.driver.title
        QuitUrl = self.driver.current_url
        # 打印当前页面的标题和url
        print(Quittitle)
        print(QuitUrl)
        # 判断页面的标题和url是否正确
        assert Quittitle == "博客登陆页"
        assert QuitUrl == "http://8.137.19.140:9090/blog_login.html"
        # 判断一下账号输入框和密码输入框的内容是否为空
        Quitusername = self.driver.find_element(By.CSS_SELECTOR, "#username").text
        Quitpassword = self.driver.find_element(By.CSS_SELECTOR, "#password").text
        # 打印账号和密码
        print("Quitusername" + Quitusername)
        print("Quitpassword" + Quitpassword)
        # 判断账号和密码是否为空
        assert Quitusername == ""
        assert Quitpassword == ""
